﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.Collections;
using WCFAuthorization.Service;

namespace WCFAuthorization.Server
{
    public partial class FormUserManager : Form
    {
        public FormUserManager(bool power)
        {
            InitializeComponent();
            if (!power)
            {
                Cacher.LoadBlackList();
                var query = Cacher.UsersCache.Where(p => !Cacher.BlackList.Contains(p.UserName));
                MyListView.AddObjects(query.ToArray());
            }
            else
            {
                MyListView.AddObjects(Cacher.UsersCache);
            }
        }

        private void FormUserManager_Load(object sender, EventArgs e)
        {
            
        }

        private void ckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (ckAll.Checked)
                MyListView.CheckObjects(MyListView.Objects);
            else
                MyListView.UncheckObjects(MyListView.Objects);
        }

        bool update = false;

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (FormAddUser form = new FormAddUser())
            {
                form.NewUser += delegate(User user)
                {
                    Cacher.UsersCache.Add(user);
                    MyListView.AddObject(user);
                    update = true;
                };
                form.ShowDialog();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DialogResult diag = MessageBox.Show("是否确认删除所选用户？",
                "提示",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
            if (diag == DialogResult.No)
                return;

            ArrayList removes = new ArrayList();
            foreach (var p in MyListView.Objects)
            {
                if (MyListView.IsChecked(p))
                {
                    Cacher.UsersCache.Remove((User)p);
                    removes.Add(p);
                    update = true;
                }
            }
            MyListView.RemoveObjects(removes);
        }

        private void FormUserManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(update == true)
               Cacher.SaveUsers();
        }

        private void MyListView_CellEditFinishing(object sender, BrightIdeasSoftware.CellEditEventArgs e)
        {
            User pUser = e.RowObject as User;
            if (pUser != null
                && e.Value!=e.NewValue)
            {
                switch (e.SubItemIndex)
                {
                    case 1:
                        pUser.ExpiredTime = Convert.ToDateTime(e.NewValue);
                        break;
                    case 2:
                        pUser.Enable = Convert.ToBoolean(e.NewValue);
                        break;
                    case 3:
                        pUser.Tag = Convert.ToString(e.NewValue);
                        break;
                }
                update = true;
            }
        }

    }
}
